package com.example.demo.controller;

import com.example.demo.model.User;
import com.example.demo.model.User2;
import com.example.demo.model.UserCard;
import com.example.demo.model.UserExtra;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;

/**
 * 响应数据
 */
@RequestMapping("/response")
@RestController
public class ResponseController {
    // -------------- 以下为返回数据 --------------
    // 返回文本
    @GetMapping("/text")
    public String responseText() {
        return "hello";
    }

    // 返回对象
    @GetMapping("/object")
    public User responseObject() {
        return User.builder().name("zhangsan").age(20).build();
    }

    // 返回复杂对象
    @GetMapping("/complex")
    public User2 responseComplex() {
        return User2.builder()
                .name("zhangsan")
                .age(20)
                .extra(UserExtra.builder().college("HUST").degree("bachelor").build())
                .cards(new UserCard[]{
                        UserCard.builder().bank("ICBC").cardNo("123456").build(),
                        UserCard.builder().bank("ABC").cardNo("654321").build()
                })
                .build();
    }

    // 返回字符串数组
    @GetMapping("/arrayString")
    public String[] responseArrayString() {
        return new String[]{"zhangsan", "lisi"};
    }

    // 返回对象数组
    @GetMapping("/arrayObject")
    public User[] responseArray() {
        return new User[]{
                User.builder().name("zhangsan").age(20).build(),
                User.builder().name("lisi").age(22).build()
        };
    }

    // 返回文件
    @GetMapping("/file")
    public ResponseEntity<byte[]> responseFile() throws IOException {
        // 读取文件
        Path path = Paths.get("a.txt");
        byte[] fileContent = Files.readAllBytes(path);

        // 创建响应头，设置文件名和文件类型
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=a.txt");
        headers.add(HttpHeaders.CONTENT_TYPE, "text/plain");

        // 返回文件内容和响应头
        return new ResponseEntity<>(fileContent, headers, HttpStatus.OK);
    }

    // ------------------- 以下为其他方式返回数据 -------------------
    // 语法为java9的语法，java8需要替换成new HashMap<>() 或 new ArrayList<>()
    // 返回Map
    @GetMapping("/map")
    public Map<String, Object> responseMap() {
        return Map.of("name", "zhangsan", "age", 20);
    }

    // 返回List
    @GetMapping("/list")
    public List<String> responseList() {
        return List.of("zhangsan", "lisi");
    }

    // 返回List
    @GetMapping("/list2")
    public List<Map<String, String>> responseList2() {
        return List.of(
                Map.of("name", "zhangsan", "age", "20"),
                Map.of("name", "lisi", "age", "22")
        ).reversed();
    }

    // 返回Map(带List)
    @GetMapping("/object2")
    public Map<String, Object> responseObject3() {
        return Map.of("name", "zhangsan", "age", 20, "list", List.of("zhangsan", "lisi"));
    }

    // ------------------ 以下为返回其他信息 ------------------
    // 返回状态码
    @GetMapping("/status")
    public ResponseEntity<String> responseStatus() {
        return new ResponseEntity<>("Custom status message", HttpStatus.I_AM_A_TEAPOT);
    }

    // 设置cookie
    @GetMapping("/cookie")
    public ResponseEntity<String> responseCookie() {
        return ResponseEntity.ok()
                .header("Set-Cookie", "name=zhangsan")
                .body("hello");
    }
}
